package com.jianneng.jdg.model.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.com.jianneng.jdg.R;
import com.jianneng.jdg.model.user.ReleaseInfo;

public class DetailAdapter extends BaseAdapter {
	private List<ReleaseInfo> list = new ArrayList<ReleaseInfo>();
	private LayoutInflater mInflater;
	private Context context;
	final int VIEW_TYPE = 3;
	final int TYPE_1 = 0;
	final int TYPE_2 = 1;
	final int TYPE_3 = 2;
	private FrameLayout mCommentSend;
	public DetailAdapter(Context context, List<ReleaseInfo> list,FrameLayout mCommentSend) {
		this.list = list;
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.mCommentSend = mCommentSend;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	// 每个convert view都会调用此方法，获得当前所需要的view样式
	@Override
	public int getItemViewType(int position) {
		int p = position;
		if (p == 0){
			return TYPE_1;
		}
		else {
			return TYPE_2;
		}
	}
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder2 mHolder2 = null;
		ViewHolder1 mHolder1 = null;
		int type = getItemViewType(position);
		// 解决抢占焦点问题
		// ViewGroup vg = (ViewGroup) convertView;
		// vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		if (convertView == null) {
			switch (type) {
			case TYPE_1:
				mHolder1 = new ViewHolder1();
				convertView = mInflater.inflate(R.layout.detail_listview_top,
						null);
				mHolder1.detail_item_userimage = (ImageView) convertView
						.findViewById(R.id.detail_item_userimage);
				mHolder1.detail_item_username = (TextView) convertView
						.findViewById(R.id.detail_item_username);
				mHolder1.detail_item_comment = (ImageView) convertView
						.findViewById(R.id.detail_item_comment);
				convertView.setTag(mHolder1);
				mHolder1.detail_item_comment.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Toast.makeText(context, "评论", 1).show();
						mCommentSend.setVisibility(View.VISIBLE);
					}
				});
				break;
			case TYPE_2:
				mHolder2 = new ViewHolder2();
				convertView = mInflater.inflate(
						R.layout.myrelease_listview_item, null);
				mHolder2.myrelease_image = (ImageView) convertView
						.findViewById(R.id.myrelease_image);
				mHolder2.release_username = (TextView) convertView
						.findViewById(R.id.release_username);
				mHolder2.release_content = (TextView) convertView
						.findViewById(R.id.release_content);
				mHolder2.delete_content = (ImageView) convertView
						.findViewById(R.id.delete_content);
				convertView.setTag(mHolder2);
				break;
			

			default:
				break;
			}

		} else {
			switch (type) {
			case TYPE_1:
				mHolder1 = (ViewHolder1) convertView.getTag();
				break;
			case TYPE_2:
				mHolder2 = (ViewHolder2) convertView.getTag();
				break;
			}
		}
		// 设置资源
		 ReleaseInfo info = list.get(position);
		switch (type) {
		case TYPE_1:
			
			break;
		case TYPE_2:
			if (position == 0) {
				mHolder2.myrelease_image
						.setImageResource(R.drawable.dialogue_message);
			} else if (position == 1) {
				mHolder2.myrelease_image
						.setImageResource(R.drawable.notification_message);
			}

			mHolder2.release_username.setText(info.username);
			mHolder2.release_content.setText(info.repease_content);
			mHolder2.delete_content.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(context, position + "被刪除了", 1).show();
					notifyDataSetChanged();
				}
			});
			break;

		default:
			break;
		}
		
		return convertView;
	}

	public class ViewHolder2 {
		ImageView myrelease_image;
		TextView release_username;
		TextView release_content;
		ImageView delete_content;
	}

	public class ViewHolder1 {
		ImageView detail_item_userimage;
		TextView detail_item_username;
		ImageView detail_item_comment;
	}


}
